import { service } from './service'
import { config } from './config'

const { default_headers } = config

const request = (option: any) => {
  const {
    url,
    method,
    params,
    data,
    headersType,
    responseType,
    withCredentials
  } = option
  return service({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    withCredentials: withCredentials,
    headers: {
      'Content-Type': headersType || default_headers
    }
  })
}

export default {
  get: async <T = any>(option: any) => {
    const res = await request({ method: 'GET', ...option })
    return res.data ? (res.data as unknown as T) : res
  },
  post: async <T = any>(option: any) => {
    const res = await request({ method: 'POST', ...option })
    return res.data ? (res.data as unknown as T) : res
  },
  curGet: async (option: any) => {
    const res = await request({ method: 'GET', ...option })
    return res
  },
  curPost: async (option: any) => {
    const res = await request({ method: 'POST', ...option })
    return res
  },
  delete: async <T = any>(option: any) => {
    const res = await request({ method: 'DELETE', ...option })
    return res.data ? (res.data as unknown as T) : res
  },
  put: async <T = any>(option: any) => {
    const res = await request({ method: 'PUT', ...option })
    return res.data ? (res.data as unknown as T) : res
  },
  // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
  getCode: async <T = any>(option: any) => {
    return await request({ method: 'GET', ...option })
  },
  // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars
  postCode: async <T = any>(option: any) => {
    return await request({ method: 'POST', ...option })
  },
  deleteCode: async <T = any>(option: any) => {
    return await request({ method: 'DELETE', ...option })
  },
  putCode: async <T = any>(option: any) => {
    return await request({ method: 'PUT', ...option })
  },
}
